package com.app.twth.ui.b2ctrailer.submit;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemTrailerSubmitListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.ui.b2ctrailer.B2CTrailer;

import java.util.ArrayList;
import java.util.List;


public class B2CTrailerSubmitAdapter extends RecyclerView.Adapter<B2CTrailerSubmitAdapter.MyViewHolder> {
    private int index = 1;
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;
    private List<B2CTrailer.DetailListDTO> data = new ArrayList<>();

    public B2CTrailerSubmitAdapter(Context mContext, List<B2CTrailer.DetailListDTO> data, MyOnClickListener myOnClickListener) {
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
    }

    public void setData(List<B2CTrailer.DetailListDTO> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemTrailerSubmitListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemTrailerSubmitListBinding itemBinding;

        public MyViewHolder(ItemTrailerSubmitListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            B2CTrailer.DetailListDTO pick = data.get(position);
            Log.e(">>>", "index:" + index);
            itemBinding.tvItem.setText(pick.getObLpn());
            itemBinding.ivDelete.setOnClickListener(v -> myOnClickListener.OnClick(position));

        }
    }

}
